﻿(function($) {
	$.widget( "ui.rioDistrict", {
		options : {
        },
        
        _create : function() {
            var $self = this;
            var element = this.element;
            var pSelector = this.pSelector = element.find('.ds-p');
            var cSelector = this.cSelector = element.find('.ds-c');
            var tSelector = this.tSelector = element.find('.ds-t');
            
            // 初始化省份
            
            $self._initP();
            
            pSelector.bind('mouseenter.rio', function(event) {
                $(this).addClass('ds-hover');
                $(this).find('.ds-selected').css('zIndex', 100);
                $(this).find('.ds-list').show();
            }).bind('mouseleave.rio', function(event) {
                $(this).removeClass('ds-hover');
                $(this).find('.ds-selected').css('zIndex', 1);
                $(this).find('.ds-list').hide();
            });
            
            cSelector.bind('mouseenter.rio', function(event) {
                $(this).addClass('ds-hover');
                $(this).find('.ds-selected').css('zIndex', 100);
                $(this).find('.ds-list').show();
                $(this).find('.ds-list').css('left', 0-pSelector.find('.ds-selected').position().left);
            }).bind('mouseleave.rio', function(event) {
                $(this).removeClass('ds-hover');
                $(this).find('.ds-selected').css('zIndex', 1);
                $(this).find('.ds-list').hide();
            });
            
            tSelector.bind('mouseenter.rio', function(event) {
                $(this).addClass('ds-hover');
                $(this).find('.ds-selected').css('zIndex', 100);
                $(this).find('.ds-list').show();
                $(this).find('.ds-list').css('left', 0-pSelector.find('.ds-selected').position().left);
            }).bind('mouseleave.rio', function(event) {
                $(this).removeClass('ds-hover');
                $(this).find('.ds-selected').css('zIndex', 1);
                $(this).find('.ds-list').hide();
            });
            
            // 设置options
            $self._setOptions($self.options);
        },
        
        _initP: function(){
            var $self = this;
            var pSelector = $self.pSelector;
            var cSelector = $self.cSelector;
            var tSelector = $self.tSelector;
            pSelector.find('.ds-item').empty();
            for(var i=0; i<rio.districtData.length; i++){
                var area = rio.districtData[i];
                if(area.c == '110000' || area.c == '120000' || area.c == '310000' || area.c == '500000') {
                    pSelector.find('.ds-item:eq(0)').append('<label><a href="javascript:void(0)" value="' + area.c + '">' + area.n + '</a></label>');
                } else if(area.c == '710000' || area.c == '810000' || area.c == '820000' ){
                    pSelector.find('.ds-item:eq(2)').append('<label><a href="javascript:void(0)" value="' + area.c + '">'+ area.n +'</a></label>');
                }else if(area.c % 10000 == 0) {
                    pSelector.find('.ds-item:eq(1)').append('<label><a href="javascript:void(0)" value="' + area.c + '">'+ area.n +'</a></label>');
                }
            }
            pSelector.find('.ds-item a').each(function(index, item) {
                $(item).bind('click.rio', function(event) {
                    var pc = $(item).attr('value');
                    var pn = $(item).html();
                    pSelector.find('.ds-selected').attr('value', pc);
                    pSelector.find('.ds-selected').html(pn);
                    pSelector.find('.ds-list').hide();
                    $self.options.code = pc; // 重置为O
                    $self._initC(pc);
                    cSelector.mouseenter();
                });
            });
        },
        _initC: function(pCode){
            var $self = this;
            var cSelector = $self.cSelector;
            var tSelector = $self.tSelector;
            
            cSelector.find('.ds-item').empty();
            tSelector.find('.ds-item').empty();
            cSelector.find('.ds-selected').attr('value', -1);
            cSelector.find('.ds-selected').html('市/区');
            tSelector.find('.ds-selected').attr('value', -1);
            tSelector.find('.ds-selected').html('区/县');
            for(var i=0; i<rio.districtData.length; i++){
                var area = rio.districtData[i];
                if(area.c % 10000 != 0 && area.c % 100 == 0 && parseInt(area.c / 10000) == pCode / 10000) {
                    cSelector.find('.ds-item').append('<label><a href="javascript:void(0)" value="' + area.c + '">' + area.n + '</a></label>');
                }
            }
            cSelector.find('.ds-item a').each(function(index, item) {
                $(item).bind('click.rio', function(event) {
                    var cc = $(item).attr('value');
                    var cn = $(item).html();
                    cSelector.find('.ds-selected').attr('value', cc);
                    cSelector.find('.ds-selected').html(cn);
                    cSelector.find('.ds-list').hide();
                    $self.options.code = cc; // 重置为O
                    $self._initT(cc);
                    tSelector.mouseenter();
                });
            });
        },
        _initT: function(cCode){
            var $self = this;
            var tSelector = $self.tSelector;
            
            tSelector.find('.ds-item').empty();
            tSelector.find('.ds-selected').attr('value', -1);
            tSelector.find('.ds-selected').html('区/县');
            for(var i=0; i<rio.districtData.length; i++){
                var area = rio.districtData[i];
                if(area.c % 10000 != 0 && area.c % 100 != 0 && parseInt(area.c / 100) == cCode / 100) {
                    tSelector.find('.ds-item').append('<label><a href="javascript:void(0)" value="' + area.c + '">' + area.n + '</a></label>');
                }
            }
            tSelector.find('.ds-item a').each(function(index, item) {
                $(item).bind('click.rio', function(event) {
                    var tc = $(item).attr('value');
                    var tn = $(item).html();
                    tSelector.find('.ds-selected').attr('value', tc);
                    tSelector.find('.ds-selected').html(tn);
                    tSelector.find('.ds-list').hide();
                    $self.options.code = tc;
                });
            });
        },
        _setOption : function(key, value) {
            var $self = this;
            var pSelector = $self.pSelector;
            var cSelector = $self.cSelector;
            var tSelector = $self.tSelector;
            switch (key) {
            case 'code':
                pSelector.find('.ds-item a').each(function(index, item){
                    if($(item).attr('value') == parseInt(value / 10000) * 10000) {
                        $(item).trigger('click.rio');
                        pSelector.trigger('mouseleave.rio');
                        return;
                    }
                });
                cSelector.find('.ds-item a').each(function(index, item){
                    if($(item).attr('value') == parseInt(value / 100) * 100) {
                        $(item).trigger('click.rio');
                        cSelector.trigger('mouseleave.rio');
                        return;
                    }
                });
                tSelector.find('.ds-item a').each(function(index, item){
                    if($(item).attr('value') == parseInt(value)) {
                        $(item).trigger('click.rio');
                        tSelector.trigger('mouseleave.rio');
                        return;
                    }
                });
                break;
            default:
                break;
            }
            return this;
        }
	});

})(jQuery);
